package com.apache.portal.common.restfull;

import com.apache.security.util.SecurityHttpServletRequest;
import com.apache.security.util.SecurityHttpServletResponse;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class ResultFullFilter implements Filter {

    private final static String SERVLET_PACKAGE = "servletPackage";

    private ServletContainer servletContainer;

    private ServletContext servletContext;

    public void init(FilterConfig filterConfig) throws ServletException {
        String servletPackageStr = filterConfig.getInitParameter(SERVLET_PACKAGE);
        boolean mark = false;
        if (StrUtil.isNotNull(servletPackageStr)) {
            String[] servletPackages = servletPackageStr.split(",");
            servletContainer = new ResultServletContainer(servletPackages);
            servletContainer.initContainer();
            mark = true;
        } else {
            String servletClassStr = filterConfig.getInitParameter("servletClass");
            if (StrUtil.isNotNull(servletClassStr)) {
                String[] servletClasses = servletClassStr.split(",");
                servletContainer = new AnnotationServletContainer();
                servletContainer.initContainer(servletClasses);
                mark = true;
            }
        }
        if (!mark) {
            throw new RuntimeException("ResultFullFilter过滤器初始化参数［servletPackage/servletClass］不能为空");
        }

        servletContext = filterConfig.getServletContext();
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        String uri = httpReq.getServletPath();
        ResultFullServletSupper servletBean = servletContainer.getServlet(uri);
        boolean isOpenSecurity = false;
        if ("true".equalsIgnoreCase(
                ToolsUtil.getInstance().getValueByKey("is_open_security_filter"))) {//是否开启安全过滤
            isOpenSecurity = true;
        }
        if (servletBean == null) {
            if (isOpenSecurity) {
                chain.doFilter(new SecurityHttpServletRequest(httpReq),
                        new SecurityHttpServletResponse((HttpServletResponse) response));
            } else {
                chain.doFilter(request, response);
            }
        } else {
            HttpServlet servlet = servletBean.getServlet();
            if (servlet.getServletConfig() == null) { //第一次要调用servlet的init方法，注入自定义的ServletConfig
                servlet.init(new ResultFullServletConfig(servletBean.getName(), servletContext,
                        new HashMap<String, String>()));
            }
            //初始化rest参数
            String[] params = servletContainer.getParamsInUri(uri);
            for (int i = 0, l = params.length; i < l; i++) {
                String p = params[i];
                request.setAttribute("PathAttr" + i, p);
            }
            if (isOpenSecurity) {
                servlet.service(new SecurityHttpServletRequest(httpReq),
                        new SecurityHttpServletResponse((HttpServletResponse) response));
            } else {
                servlet.service(request, response);
            }
        }
    }

    public void destroy() {
        System.out.println("----------------ResultFullFilter destroy----------------");
    }

}
